/**
 * @project name:mail
 */
package com.situ.mail.shop.controller;

import com.situ.mail.base.BaseController;
import com.situ.mail.shop.pojo.Directory;
import com.situ.mail.shop.pojo.Good;
import com.situ.mail.shop.service.DirectoryService;
import com.situ.mail.shop.service.GoodService;
import com.situ.mail.shop.service.UploadService;
import com.situ.mail.utils.Pager;
import com.situ.mail.utils.SystemContext;
import com.situ.mail.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Author : yun&bo
 * @CreateDate : ==>2020/12/28 10:01
 * @ClassName : ==>
 * @Description : ==>
 * @Version
 */
@Controller
@RequestMapping("/good")
public class GoodController extends BaseController {
    private static final String GOOD_PAGERS = "pagers";
    private static final String GOOD_OBJ = "obj";
    private static final String GOOD_LIST = "good/good_list";
    private static final String GOOD_LIST_GO = "good/findGood";
    private static final String GOOD_ADD_LIST = "good/good_add";
    private static final String GOOD_EDIT_LIST = "good/good_edit";


    @Autowired
    private GoodService goodService;
    @Autowired
    private DirectoryService directoryService;

    @Autowired
    private UploadService uploadService;

    /**
     * 进行模糊查询
     *
     * @param modelAndView
     * @param good
     * @return
     */
    @RequestMapping("findGood")
    public ModelAndView findGoods(ModelAndView modelAndView, Good good) {
        Pager<Good> pager = goodService.findByEntity(good);
        modelAndView.addObject(GOOD_PAGERS, pager);
        modelAndView.addObject(GOOD_OBJ, good);
        modelAndView.setViewName(GOOD_LIST);
        return modelAndView;
    }


    @RequestMapping("goEdit")
    public ModelAndView goodEdit(ModelAndView modelAndView, Long rowId) {
        Good good = goodService.getById(rowId);
        modelAndView.addObject(GOOD_OBJ, good);
        modelAndView.setViewName(GOOD_EDIT_LIST);
        return modelAndView;
    }


    @RequestMapping("edit")
    public ModelAndView edit(ModelAndView modelAndView, Good good) {
        goodService.updateById(good);
        modelAndView.setViewName("redirect:/" + GOOD_LIST_GO);
        return modelAndView;
    }


    @RequestMapping("goAdd")
    public ModelAndView goAdd(ModelAndView modelAndView, Good good) {
        List<Directory> directory = directoryService.listAll();
        modelAndView.addObject("goodType", directory);
        modelAndView.setViewName(GOOD_ADD_LIST);
        return modelAndView;
    }


    /**
     *  百度的U 上传 注意不要和属性重名啊   ！！！ 类目
     * @param modelAndView
     * @param good
     * @param files
     * @param request
     * @return
     */
    @RequestMapping("add")
    public ModelAndView add(ModelAndView modelAndView, Good good, @RequestParam("file") MultipartFile[] files, HttpServletRequest request) {
        String filePath =   uploadService.doUploadFile(files[0],2);
        good.setGoodImage(filePath);
        //这是此商品的数量，然后的得到他的父亲，给他的父亲个数+goodNumber，递归找到父亲为-1的为止
        Integer goodNewNumber = good.getGoodInventoryNumber();
        Long goodDirectory = good.getDirId();
        diGuiFindFather(goodNewNumber,goodDirectory);
        goodService.insert(good);
        modelAndView.setViewName("redirect:/" + GOOD_LIST_GO);
        System.out.println("你好");
        return modelAndView;
    }


    @RequestMapping("delete")
    public ModelAndView delete(ModelAndView modelAndView, Good good) {
        goodService.deleteById(good);
        modelAndView.setViewName("redirect:/" + GOOD_LIST_GO);
        return modelAndView;
    }


    /**
     *   根据商品递归增加文件夹
     * @param goodNewNumber
     * @param goodDirectory
     * @return
     */
    public int diGuiFindFather(int goodNewNumber, Long goodDirectory) {
        //递归进行，到  - 1 停止
        Directory directory = directoryService.getById(goodDirectory);
        directory.setGoodCount(directory.getGoodCount() + goodNewNumber);
        directoryService.update(directory);
        if ( !isEmpty(directory.getFatherId()) && directory.getFatherId() != -1L) {
            diGuiFindFather(goodNewNumber, directory.getFatherId());
        }
        return 0;
    }








}
